package com.physique.tea.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.physique.tea.Pojo.History;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface HistoryMapper extends BaseMapper<History> {
    // 增
    @Insert("INSERT INTO HISTORY (ID, PHYNAME, USERNAME, DAY) VALUES (#{ID}, #{PHYNAME}, #{USERNAME}, #{DAY})")
    @Options(useGeneratedKeys = true, keyProperty = "ID")
    int insertHistory(History history);

    // 删
    @Delete("DELETE FROM HISTORY WHERE ID = #{ID}")
    int deleteById(@Param("ID") Integer ID);

    // 改
    @Update("UPDATE HISTORY SET PHYNAME = #{PHYNAME}, USERNAME = #{USERNAME}, DAY = #{DAY} WHERE ID = #{ID}")
    int updateHistory(History history);

    // 查
    @Select("SELECT ID, PHYNAME, USERNAME, DAY FROM HISTORY WHERE ID = #{ID}")
    History selectById(@Param("ID") Integer ID);

    @Select("SELECT ID, PHYNAME, USERNAME, DAY FROM HISTORY WHERE USERNAME = #{USERNAME} ORDER BY DAY DESC")
    List<History> selectByUsername(@Param("USERNAME") String USERNAME);

    @Select("SELECT ID, PHYNAME, USERNAME, DAY FROM HISTORY WHERE PHYNAME = #{PHYNAME}")
    List<History> selectByPhyname(@Param("PHYNAME") String PHYNAME);

    @Select("SELECT ID, PHYNAME, USERNAME, DAY FROM HISTORY WHERE DAY BETWEEN #{START} AND #{END}")
    List<History> selectByDateRange(@Param("START") Date START, @Param("END") Date END);

    @Select("SELECT PHYNAME, COUNT(*) AS COUNT FROM HISTORY GROUP BY PHYNAME")
    List<Map<String, Object>> countByPhyname();
}